package pl.PawelAndAlicja.badugiproject.main;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class kli extends Frame implements ActionListener{

    Label info;
    Label info2;
    Label output;
    Label output2;
    Label output3;
    Label output4;
    Button button;
    TextField input;
    Socket socket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    kli()
    {
        setSize(400,400);
        setFont(new Font(Font.SANS_SERIF,Font.PLAIN,30));
        input = new TextField(20);   // wpisz tekst
        output = new Label();
        output.setBackground(Color.white);
        output2 = new Label();
        output2.setBackground(Color.white);
        output3 = new Label();
        output3.setBackground(Color.white);
        output4 = new Label();
        output4.setBackground(Color.white);
        info = new Label("Twoje Karty: ");
        //  info2 = new Label("R - rozklad? Q- zakoncz program");
        button = new Button("Rozdaj");
        button.addActionListener(this);

        setLayout(new GridLayout(7,1));
        add(info);
        //  add(info2);
        add(input);
        add(button);
        add(output);
        add(output2);
        add(output3);
        add(output4);
    }

    public void actionPerformed(ActionEvent event){

        if(event.getSource() == button){
            out.println(input.getText());
            try{
                output.setText(in.readLine());
                output2.setText(in.readLine());
                output3.setText(in.readLine());
                //      output4.setText(in.readLine());
            }
            catch (IOException e){
                System.out.println("Read failed"); System.exit(1);
            }
            input.setText("");
            //        input.requestFocus();
        }
    }

    public void listenSocket(){

        try
        {
            socket = new Socket("localhost", 345);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        }
        catch (UnknownHostException e)
        {
            System.out.println("Unknown host: localhost"); System.exit(1);
        }
        catch  (IOException e)
        {
            System.out.println("No I/O"); System.exit(1);
        }
    }
    //watek.start();


    public static void main(String[] args){
        kli frame = new kli();
        frame.setBounds(100, 100, 800, 500);
        frame.addWindowListener( new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        } );
        frame.pack();
        frame.setVisible(true);
        frame.listenSocket();

    }

}